package com.github.wui.circle;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.github.wui.R;
import com.github.wui.utils.ViewUtils;

import java.util.ArrayList;
import java.util.List;

public class CircleLinearLayout extends LinearLayout {

    /* ================== 原字段不变 ================== */
    private float round;
    private int bkColor;
    private boolean isOpenGradient = false;
    private int startColor;
    private int centerColor;
    private int endColor;
    private boolean mut_grandiet = false;
    private String mut_grandiet_colors;
    private int grandientType = 0;
    private List<Integer> colors = new ArrayList<>();
    private int linearGranditentDir = L_R;
    private int borderColor = Color.TRANSPARENT;
    private float borderWidth = 0;
    private boolean isCloseFullRoundMode = false;
    private float leftTopRound = 0f;
    private float leftBottomRound = 0f;
    private float rightTopRound = 0f;
    private float rightBottomRound = 0f;

    /* ================== 新增 ================== */
    private GradientDrawable mGradientDrawable;   // 永不置空，只更新属性

    /* ================== 常量 ================== */
    private static final int Linear = 0;
    private static final int T_B = 0;
    private static final int L_R = 1;
    private static final int B_T = 2;
    private static final int R_L = 3;
    private static final int BR_TL = 4;
    private static final int BL_TR = 5;
    private static final int TL_BR = 6;
    private static final int TR_BL = 7;

    public CircleLinearLayout(Context context) {
        this(context, null);
    }

    public CircleLinearLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CircleRelativeLayout);
        round = a.getDimension(R.styleable.CircleRelativeLayout_round, 0);
        bkColor = a.getColor(R.styleable.CircleRelativeLayout_bkColor, Color.WHITE);
        startColor = a.getColor(R.styleable.CircleRelativeLayout_startColor, -1);
        centerColor = a.getColor(R.styleable.CircleRelativeLayout_centerColor, -1);
        endColor = a.getColor(R.styleable.CircleRelativeLayout_endColor, -1);
        isOpenGradient = a.getBoolean(R.styleable.CircleRelativeLayout_isOpenGradient, false);
        grandientType = a.getInt(R.styleable.CircleRelativeLayout_gradientType, Linear);
        linearGranditentDir = a.getInt(R.styleable.CircleRelativeLayout_linear_dir, L_R);
        mut_grandiet = a.getBoolean(R.styleable.CircleRelativeLayout_mut_grandiet, false);
        mut_grandiet_colors = a.getString(R.styleable.CircleRelativeLayout_mur_grandient_color);

        borderColor = a.getColor(R.styleable.CircleRelativeLayout_wui_border_color, Color.TRANSPARENT);
        borderWidth = a.getDimension(R.styleable.CircleRelativeLayout_wui_border_width, 0f);

        rightBottomRound = a.getDimension(R.styleable.CircleTextView_rightBottomRound, 0f);
        leftBottomRound = a.getDimension(R.styleable.CircleTextView_leftBottomRound, 0f);
        rightTopRound = a.getDimension(R.styleable.CircleTextView_rightTopRound, 0f);
        leftTopRound = a.getDimension(R.styleable.CircleTextView_leftTopRound, 0f);
        isCloseFullRoundMode = a.getBoolean(R.styleable.CircleRelativeLayout_isCloseFullRoundMode, false);
        a.recycle();

        setWillNotDraw(false);
        mGradientDrawable = new GradientDrawable();   // 初始化
        refreshBackground();                          // 第一次刷背景
    }

    /* ================== 唯一刷新入口 ================== */
    private void refreshBackground() {
        if (isOpenGradient) {
            int[] colorArr;
            if (mut_grandiet) {
                String[] splits = mut_grandiet_colors.split(";");
                colorArr = new int[splits.length];
                for (int i = 0; i < splits.length; i++) {
                    colorArr[i] = Color.parseColor(splits[i]);
                }
            } else {
                colorArr = defaultColors();
            }
            mGradientDrawable.setColors(colorArr);
            mGradientDrawable.setGradientType(grandientType);
            mGradientDrawable.setOrientation(orientationFromDir(linearGranditentDir));
        } else {
            mGradientDrawable.setColor(bkColor);
        }

        if (isCloseFullRoundMode) {
            ViewUtils.setRound(mGradientDrawable, leftTopRound, rightTopRound,
                    rightBottomRound, leftBottomRound);
        } else {
            mGradientDrawable.setCornerRadius(round);
        }

        mGradientDrawable.setStroke((int) borderWidth, borderColor);
        setBackground(mGradientDrawable);   // 只换属性，不置空
    }

    private int[] defaultColors() {
        colors.clear();
        if (startColor != -1) colors.add(startColor);
        if (centerColor != -1) colors.add(centerColor);
        if (endColor != -1) colors.add(endColor);
        int[] arr = new int[colors.size()];
        for (int i = 0; i < colors.size(); i++) arr[i] = colors.get(i);
        return arr;
    }

    private GradientDrawable.Orientation orientationFromDir(int dir) {
        switch (dir) {
            case L_R: return GradientDrawable.Orientation.LEFT_RIGHT;
            case R_L: return GradientDrawable.Orientation.RIGHT_LEFT;
            case T_B: return GradientDrawable.Orientation.TOP_BOTTOM;
            case B_T: return GradientDrawable.Orientation.BOTTOM_TOP;
            case TL_BR: return GradientDrawable.Orientation.TL_BR;
            case TR_BL: return GradientDrawable.Orientation.TR_BL;
            case BL_TR: return GradientDrawable.Orientation.BL_TR;
            case BR_TL: return GradientDrawable.Orientation.BR_TL;
            default: return GradientDrawable.Orientation.LEFT_RIGHT;
        }
    }

    /* ================== 所有 setter 统一入口 ================== */
    public void setBkColor(int color) {
        bkColor = color;
        refreshBackground();
    }

    public int getBkColor() {
        return bkColor;
    }

    public void setRound(float round) {
        this.round = round;
        refreshBackground();
    }

    public void setOpenGradient(boolean openGradient) {
        isOpenGradient = openGradient;
        refreshBackground();
    }

    public void setStartColor(int startColor) {
        this.startColor = startColor;
        refreshBackground();
    }

    public void setCenterColor(int centerColor) {
        this.centerColor = centerColor;
        refreshBackground();
    }

    public void setEndColor(int endColor) {
        this.endColor = endColor;
        refreshBackground();
    }

    public void setMut_grandiet(boolean mut_grandiet) {
        this.mut_grandiet = mut_grandiet;
        refreshBackground();
    }

    public void setMut_grandiet_colors(String mut_grandiet_colors) {
        this.mut_grandiet_colors = mut_grandiet_colors;
        refreshBackground();          // 关键：不再 setBackground(null)
    }

    public void setGrandientType(int grandientType) {
        this.grandientType = grandientType;
        refreshBackground();
    }

    public void setLinearGranditentDir(int linearGranditentDir) {
        this.linearGranditentDir = linearGranditentDir;
        refreshBackground();
    }

    public void setBorderColor(int borderColor) {
        this.borderColor = borderColor;
        refreshBackground();
    }

    public void setBorderWidth(float borderWidth) {
        this.borderWidth = borderWidth;
        refreshBackground();
    }

    /* 其余 getter 省略，照原样即可 */
}